/**
 *
 * action.c - Copy Queue Library
 *
 * The MIT License
 *
 * Copyright (c) 2008 John Van Enk
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

#include "action.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>

/**
 * Internal prototypes to action.c. These shouldn't be used
 * elsewhere. 
 */
CPQ_Error_e_t CPQ_Action_Copy(CPQ_Action_s_t * action);
CPQ_Error_e_t CPQ_Action_Move(CPQ_Action_s_t * action);
CPQ_Error_e_t CPQ_Action_Delete(CPQ_Action_s_t * action);

/**
 * Helper functions.
 */
CPQ_Error_e_t CPQ_Copy_File(char * src, char * dst);
CPQ_Error_e_t CPQ_Copy_Dir(char * src, char * dst);

/**
 * External definitions.
 */
CPQ_Error_e_t CPQ_Perform_Action(CPQ_Action_s_t * action)
{
    CPQ_Error_e_t ret;

    switch(action->action)
    {
    case CPQ_ACTION_COPY:
        ret = CPQ_Action_Copy(action);
        break;
    case CPQ_ACTION_DELETE:
        ret = CPQ_Action_Delete(action);
        break;
    case CPQ_ACTION_MOVE:
        ret = CPQ_Action_Move(action);
        break;
    default:
        ret = CPQ_ERR_BAD_ACTION_TYPE;
        break;
    }

    return ret;
}


CPQ_Error_e_t CPQ_Copy_File(char * src, char * dst)
{
    int ret_a, ret_b;
    struct stat src_stat, dst_stat;

    ret_a = stat(src, &src_stat);
    ret_b = stat(dst, &dst_stat);

    if(-1 == ret_a)
    {
        /* The source has to exist. */
        return CPQ_ERR_BAD_SRC_PATH;
    }
    else if(!S_ISREG(src_stat.st_mode))
    {
        /* The source must be a file. */
        return CPQ_ERR_NOT_A_FILE;
    }

    if(-1 == ret_b)
    {
        switch(errno)
        {
        default:
        case ELOOP: /* Too many symbolic links. */
        case ENAMETOOLONG: /* Name too long. */
        case ENOTDIR: /* Something in the path is not a directory. */
            /* Any of the above are an unforgivable offense. */
            return CPQ_ERR_BAD_DST_PATH;
            break;
        case ENOENT:
            /* Everything is fine. */
            break;
        }
    }

    return CPQ_ERR_OK;
}

CPQ_Error_e_t CPQ_Copy_Dir(char * src, char * dst)
{
    int ret_a, ret_b;
    struct stat src_stat, dst_stat;

    ret_a = stat(src, &src_stat);
    ret_b = stat(src, &dst_stat);

    if(-1 == ret_a) return CPQ_ERR_BAD_SRC_PATH;
    if(-1 == ret_b) return CPQ_ERR_BAD_SRC_PATH;

    return CPQ_ERR_OK;
}

/**
 * Internal definitions. Don't use these outside of action.c.
 */
CPQ_Error_e_t CPQ_Action_Copy(CPQ_Action_s_t * action)
{
    // Perform copy

    return CPQ_ERR_OK;
}

CPQ_Error_e_t CPQ_Action_Move(CPQ_Action_s_t * action)
{
    // Perform move

    return CPQ_ERR_OK;
}

CPQ_Error_e_t CPQ_Action_Delete(CPQ_Action_s_t * action)
{
    return CPQ_ERR_OK;
}

